

const tools = require('../util/tool');
module.exports = class poster_sql extends require('./mysql'){


    /**
     * 车位信息获取
     * @param parkname
     * @param parkpay
     * @param parktype
     * @param parkstatus
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getParkDataSql(parkname,parkpay,parktype,parkstatus,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `park` where 1=1";
            if (parkname) sql += " and parkname like '%"+parkname+"%'";
            if (parkpay) sql += " and parkpay like '%"+parkpay+"%'";
            if (parktype) sql += " and parktype= '" + parktype + "'";
            if (parkstatus) sql += " and parkstatus= '" + parkstatus + "'";
            sql += " limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                console.log(this.formarParams(currPage,pageNum));
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 车位模糊查询分页
     * @param parkname
     * @param parkpay
     * @param parktype
     * @param parkstatus
     * @returns {Promise<any>}
     */
    static getParkDataTotal(parkname,parkpay,parktype,parkstatus){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `park` where 1=1';
            if (parkname) sql += " and parkname like '%"+parkname+"%'";
            if (parkpay) sql += " and parkpay like '%"+parkpay+"%'";
            if (parktype) sql += " and parktype= '" + parktype + "'";
            if (parkstatus) sql += " and parkstatus= '" + parkstatus + "'";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 添加车位
     * @param parkname
     * @param parkpay
     * @param parktype
     * @param parkstatus
     * @returns {Promise<any>}
     */
    static addParkDataSql(parkname,parkpay,parktype,parkstatus){

        return new Promise((resolve, reject) => {
            let sql = "insert into `park`(parkname,parkpay,parktype,parkstatus) values (?, ?, ?, ?)";
            console.log(sql)
            this.query(sql,this.formarParams(parkname,parkpay,parktype,parkstatus)).then(result=>{
                resolve('添加成功')
            }).catch(err=>{
                reject('添加失败')
            })
        })
    }


    /**
     * 删除车位
     * @param id
     * @returns {Promise<any>}
     */
    static delParkDataSql(id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from `park` where id ='+id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除车位成功')
            }).catch(err=>{
                reject('删除车位失败')
            })
        })
    }


    /**
     * 修改车位
     * @param parkname
     * @param parkpay
     * @param parktype
     * @param parkstatus
     * @returns {Promise<any>}
     */
    static editParkDataSql(u_id,parkname,parkpay,parktype,parkstatus){
        return new Promise((resolve, reject) => {
            let sql = 'update `park` set parkname="'+parkname+'", parkpay="'+parkpay+'", parktype="'+parktype+'", parkstatus="'+parkstatus+'" where id ='+u_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('修改成功')
            }).catch(err=>{
                reject('修改失败')
            })
        })
    }


    /**
     * 车位订单列表
     * @param parkname
     * @param orderpay
     * @param orderstatus
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getParkOrderSql(parkname,orderpay,orderstatus,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `park` where 1=1";
            if (parkname) sql += " and parkname like '%"+parkname+"%'";
            if (orderpay) sql += " and orderpay like '%"+orderpay+"%'";
            if (orderstatus) sql += " and orderstatus= '" + orderstatus + "'";
            sql += " limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                console.log(this.formarParams(currPage,pageNum));
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 车位列表总数
     * @param parkname
     * @param orderpay
     * @param orderstatus
     * @returns {Promise<any>}
     */
    static getParkOrderTotal(parkname,orderpay,orderstatus){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `park` where 1=1';
            if (parkname) sql += " and parkname like '%"+parkname+"%'";
            if (orderpay) sql += " and orderpay like '%"+orderpay+"%'";
            if (orderstatus) sql += " and orderstatus= '" + orderstatus + "'";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 车位订单修改
     * @param u_id
     * @param startdate
     * @param enddate
     * @param orderpay
     * @param orderstatus
     * @param note
     * @returns {Promise<any>}
     */
    static editParkOrderSql(u_id,startdate,enddate,orderpay,orderstatus,note){
        return new Promise((resolve, reject) => {
            let sql = 'update `park` set startdate="'+startdate+'", enddate="'+enddate+'", orderpay="'+orderpay+'", orderstatus="'+orderstatus+'" ,note="'+note+'" where id ='+u_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('修改成功')
            }).catch(err=>{
                reject('修改失败')
            })
        })
    }


    /**
     * 用户车位缴费
     * @param u_id
     * @param orderstatus
     * @param reply
     * @returns {Promise<any>}
     */
    static payParkOrderSql(u_id,orderstatus,reply){
        return new Promise((resolve, reject) => {
            let sql = 'update `park` set orderstatus="'+orderstatus+'" ,reply="'+reply+'" where id ='+u_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('缴费成功')
            }).catch(err=>{
                reject('缴费失败')
            })
        })
    }


    
}